package com.zx.idc.backend.gui.config.aspect;

import com.zx.idc.backend.gui.shiro.TokenManagerUtil;
import com.zx.idc.ds.sys.entity.SysUser;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * 登录拦截器
 *
 * @author rxliuli
 */
@Component
public class LoginInterceptor implements HandlerInterceptor {
    private final Logger log = LoggerFactory.getLogger(getClass());

    @Autowired
    private TokenManagerUtil tokenManagerUtil;

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws IOException {
        final SysUser sysUser = tokenManagerUtil.getCurrentUser();
        if (sysUser == null) {
            response.setStatus(400);
            response.sendRedirect("/system/login");
            return false;
        }
        log.info("logging request, url: {}", request.getRequestURI());
        return true;
    }
}
